/*
 * PPM.cpp
 *
 *  Created on: Feb 18, 2010
 *      Author: admin
 */

#include <iostream>
#include "PPM.h"

PPM::PPM() {
}

PPM::~PPM() {
}

void PPM::load(const char* fileName) {
	ifstream input;
	input.open(fileName);

	string temp;
	int row;
	int col;

	getline(input, temp); // Read "P3"
	getline(input, temp); // Read "# ..."
	input >> col >> row; // Read the size of the Matrix eg."10 10"
	getline(input, temp);
	getline(input, temp); // Read "255"

	//cout<<row<<' '<<col<<endl;

	Matrix<Pixel> m1(row, col);

	input >> m1; // Read the Matrix into m1
	m_image = m1;

	input.close();
}

void PPM::save(const char* fileName) {
	ofstream output;
	output.open(fileName);

	output << "P3" << endl;
	output << "# new image create on 2010" << endl;
	output << m_image.getColSize() << " " << m_image.getRowSize() << endl;
	output << "255" << endl;
	output << m_image;

	output.close();
}

void PPM::invert() {
	Pixel p(255, 255, 255);
	int csize = m_image.getColSize();
	int rsize = m_image.getRowSize();
	for(int i = 0; i < rsize; ++i) {
		for(int j = 0; j < csize; ++j) {
			m_image(i, j) = p - m_image(i, j);
		}
	}
}

void PPM::enlarge(int factor) {

	int rsize = factor * m_image.getRowSize();
	int csize = factor * m_image.getColSize();

	Matrix<Pixel> image(rsize, csize);

	for (int i = 0; i < rsize; ++i) {
		for (int j = 0; j < csize; ++j) {
			// cout << i / factor << " " << j / factor << endl;
			image(i, j) = m_image((i / factor), (j / factor));
		}
	}
//	cout<<image<<endl;
	m_image = image;
}
